from odoo.addons.rp_customer_management.functions.customer_functions import CustomerFunctions as Customer
from odoo.addons.rp_customer_management.functions.address_functions import AddressFunctions as Address
from odoo.addons.rp_customer_management.models.primary.rp_customer import SOURCE_TYPE, CUSTOMER_CATEGORY
from odoo.addons.rp_courier_management.models.primary.rp_courier_mail_route import FULFILLMENT_TYPE


class ShipmentFC(object):
    """
    Model:
        满足运单的所有算法逻辑

    Param:
        - env: odoo env

    Function:
        - backdoor_create_shipment: odoo后台创建运单
    """

    def __init__(self, env):
        self.env = env

    def backdoor_create_shipment(self, ship_order):
        """
        Odoo后台创建运单

        :param ship_order 订单记录(rp.ship.order)

        @return  shipment  运单记录集rp.shipment)
        """
        # 获取客户,没有则创建
        customer = Customer(self.env).get_customer(email=ship_order.email, last_name=ship_order.send_last_name, first_name=ship_order.send_first_name)
        customer.write({'source_type': SOURCE_TYPE[0][0],
                        'customer_category': CUSTOMER_CATEGORY[0][0]})

        # 检验发件地址，没有则创建
        ship_from = Address(self.env).get_address(customer, ship_order.send_first_name, ship_order.send_last_name, ship_order.send_phone, ship_order.send_address_type, ship_order.send_company_name,
                                                  ship_order.send_country_id, ship_order.send_state_id, ship_order.send_city_id, False, ship_order.send_street, ship_order.send_postal_code_id,
                                                  ship_order.send_email)
        # 检验收件地址，没有则创建
        ship_to = Address(self.env).get_address(customer, ship_order.receive_first_name, ship_order.receive_last_name, ship_order.receive_phone, ship_order.receive_address_type, ship_order.receive_company_name,
                                                ship_order.receive_country_id, ship_order.receive_state_id, ship_order.receive_city_id, False, ship_order.receive_street, ship_order.receive_postal_code_id,
                                                ship_order.receive_email)

        shipment = self._create_shipment(customer=customer, ship_order=ship_order, ship_from=ship_from, ship_to=ship_to)
        return shipment

    def _create_shipment(self, customer, ship_order, ship_from, ship_to):
        """
        创建运单，根据多包裹创建多个运单

        :param customer 客户(rp.customer)
        :param ship_order 订单(rp.ship.order)
        :param ship_from 发件地址(res.partner)
        :param ship_to 收件地址(res.partner)

        @return shipment_rcds (rp.shipment)
        """
        values = {}
        # 发件人信息
        values.update({'send_country_id': ship_from.country_id.id,  # 发件国家
                       'send_postal_code_id': ship_from.postal_code_id.id,
                       'send_state_id': ship_from.state_id.id,
                       'send_city_id': ship_from.city_id.id,
                       'send_street': ship_from.street,
                       'send_first_name': ship_from.first_name,
                       'send_last_name': ship_from.last_name,
                       'send_phone': ship_from.mobile,
                       'send_address_type': ship_from.address_type,
                       'send_company_name': ship_from.contact_company_name,
                       'send_email': ship_from.email})
        # 收件人信息
        values.update({'receive_country_id': ship_to.country_id.id,  # 收件国家
                       'receive_postal_code_id': ship_to.postal_code_id.id,
                       'receive_state_id': ship_to.state_id.id,
                       'receive_city_id': ship_to.city_id.id,
                       'receive_street': ship_to.street,
                       'receive_first_name': ship_to.first_name,
                       'receive_last_name': ship_to.last_name,
                       'receive_phone': ship_to.mobile,
                       'receive_address_type': ship_to.address_type,
                       'receive_company_name': ship_to.contact_company_name,
                       'receive_email': ship_to.email})
        # 运单信息
        values.update({'rp_ship_order_id': ship_order.id,
                       'customer_id': customer.id,  # 客户
                       'shipper_id': ship_order.shipper_id.id,  # 托运商
                       'shipper_mail_route_id': ship_order.shipper_mail_route_id.id,  # 托运商邮路
                       'package_type_id': ship_order.package_type_id.id,
                       'aging_id': ship_order.aging_id.id,
                       'active_flag': True})

        # 头尾程
        for fu in ship_order.rp_ship_order_fulfillment_line_ids:
            if fu.operation_type == FULFILLMENT_TYPE[0][0]:
                values.update({'dropoff_flag': True,  # 网点收件
                               'head_postal_outlet_id': fu.postal_outlet_id.id})  # 头程网点
            elif fu.operation_type == FULFILLMENT_TYPE[1][0]:
                values.update({'pickup_flag': True})  # 上门取件
            elif fu.operation_type == FULFILLMENT_TYPE[2][0]:
                values.update({'collect_flag': True,  # 网点取件
                               'tail_postal_outlet_id': fu.postal_outlet_id.id})  # 尾程网点
            elif fu.operation_type == FULFILLMENT_TYPE[3][0]:
                values.update({'delivery_flag': True})  # 送货上门

        # 包裹类型
        values.update({'rp_parcel_ids': [(6, 0, ship_order.rp_parcel_ids.ids)],  # 包裹明细
                       'parcel_total_weight': ship_order.parcel_total_weight,
                       'parcel_total_volume': ship_order.parcel_total_volume,
                       'parcel_total_values': ship_order.parcel_total_values,
                       'parcel_actual_weight': ship_order.parcel_actual_weight,
                       'parcel_rounded_weight': ship_order.parcel_rounded_weight})

        # 增值服务
        va_values_list = []
        for va in ship_order.rp_ship_order_value_added_line_ids:
            va_value = {'value_added_id': va.value_added_id.id,
                        'value_added_type': va.value_added_type,
                        'value_added_category':va.value_added_category,
                        'price': va.actual_price}
            va_values_list.append((0, 0, va_value))
        values.update({'rp_shipment_value_added_line_ids': va_values_list})  # 增值服务明细

        # 价格信息
        values.update({'mail_route_price': ship_order.mail_route_actual_price,
                       'fulfillment_price': ship_order.fulfillment_total_actual_price,
                       'value_added_price': ship_order.value_added_total_actual_price,
                       'total_price': ship_order.total_actual_price})

        rcd = self.env['rp.shipment'].create([values])
        return rcd

